import Crypto from "crypto-js";

// export function encryptByAES(t: any, r: string) {
//   if (!r || 16 !== r.length) throw "密钥 key 必须是十六位字符串";

//   const n = Crypto.enc.Utf8.parse(t);
//   return Crypto.AES.encrypt(n, enc.parse(r), {
//     mode: Crypto.mode.ECB,
//     padding: Crypto.pad.Pkcs7,
//   })
//     .ciphertext.toString()
//     .toUpperCase();
// }

export function useSign() {
  let timestamp = Date.parse(String(new Date()));
  timestamp = timestamp.toString().substring(0, 10) as any;
  var sign = (0, Crypto.MD5)(
    "12IX012309#JFN4S1JX9123JF" + timestamp
  ).toString();
  const [signature, nonce] = useSignature(timestamp);
  return { timestamp, sign, signature, nonce };
}

function useSignature(timestamp: number) {
  const passToken = "iCQMyvcnKw2rFhqrLjpDn1aTcT1AO1Uk";

  const n = randomChar();
  const o = (0, Crypto.SHA256)(
    timestamp + passToken + n + timestamp
  ).toString();
  return [o, n];
}

function randomChar() {
  for (
    var e = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : 10,
      t = "0123456789qwertyuioplkjhgfdsazxcvbnm",
      r = "",
      a = new Date().getTime(),
      o = 0;
    o < e;
    o++
  )
    r += t.charAt(Math.ceil(1e8 * Math.random()) % t.length);
  return a + r;
}


